package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.exception.ShoppingCartBusinessException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.aspectj.bridge.Message;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetMealMapper setMealMapper;
    @Override
    @Transactional
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart shoppingCart1 = shoppingCartMapper.selectOne(shoppingCart);
        if (shoppingCart1 != null) { //相同记录已经存在，修改shoppingCart中的总价和数量,删除对应购物车记录
            int number = shoppingCart1.getNumber();
            shoppingCart1.setNumber(++number);
            shoppingCartMapper.deleteById(shoppingCart1.getId());
        } else { //相同记录不存在，构造shoppingCart
            if(shoppingCartDTO.getDishId()!=null&&shoppingCartDTO.getSetmealId()==null) { //需要新增的是菜品
                DishVO dishVO = dishMapper.searchDishById(shoppingCartDTO.getDishId());
                shoppingCart.setName(dishVO.getName());
                shoppingCart.setImage(dishVO.getImage());
                shoppingCart.setAmount(dishVO.getPrice());
            } else if(shoppingCartDTO.getDishId()==null&&shoppingCartDTO.getSetmealId()!=null){ //需要新增的是套餐
                SetmealVO setmealVO = setMealMapper.selectSetmealById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmealVO.getName());
                shoppingCart.setImage(setmealVO.getImage());
                shoppingCart.setAmount(setmealVO.getPrice());

            } else { //前端传输信息有误
                throw new ShoppingCartBusinessException(MessageConstant.SHOPPING_CART_ADD_EXCEPTION);
            }
            shoppingCart1=shoppingCart;
            shoppingCart1.setNumber(1);
            shoppingCart1.setCreateTime(LocalDateTime.now());
        }
        shoppingCartMapper.insertOne(shoppingCart1);//插入对应购物车条目
    }

    @Override
    public List<ShoppingCart> list() {
        Long userId = BaseContext.getCurrentId();
        return shoppingCartMapper.selectByUserId(userId);
    }

    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.clean(userId);
    }

    @Override
    @Transactional
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCart = shoppingCartMapper.selectOne(shoppingCart);
        int number = shoppingCart.getNumber();//获取该商品在购物车中的数量
        shoppingCartMapper.deleteById(shoppingCart.getId());//删除原先的条目
        if(number>1) { //该商品的数量不止一个，需要将数量减一再加入数据库表格中
            shoppingCart.setNumber(--number);
            shoppingCartMapper.insertOne(shoppingCart);//再添加新的条目
        }
    }
}
